package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import model.Employee;
import view.PanelNhanVien;

public class EmployeeController implements ActionListener, MouseListener {
	private PanelNhanVien view;
	private ResultSet rs;
	private Statement statement;
	private Connection connect;

	public EmployeeController(PanelNhanVien view) {
		this.view = view;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}

	public void loadData(Vector<Object> data) {
		try {
			connect = model.Database.connect();

			statement = connect.createStatement();
			String sql = "select * from employee";
			rs = statement.executeQuery(sql);

			while (rs.next()) {
				Employee e = new Employee();
				e.setId(rs.getString("id_empl"));
				e.setName(rs.getString("name"));
				e.setBirthday(rs.getDate("birthday"));
				e.setSex(rs.getString("sex"));
				e.setIdent(rs.getString("identification"));
				e.setAddress(rs.getString("address"));
				e.setPhone(rs.getString("phone"));
				e.setDateBegin(rs.getDate("date_begin"));
				e.setRegency(rs.getString("regency"));
				data.add(e);
			}

			view.setTableModel(data);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				statement.close();
				connect.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		view.showDetail();
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
